<?php
namespace Home\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Home\Model\Home;
use Home\Form\HomeForm;

class HomeController extends AbstractActionController
{
    protected $homeTable;

    public function getHomeTable()
    {
        if (!$this->homeTable) {
            $sm = $this->getServiceLocator();
            $this->homeTable = $sm->get('Home\Model\HomeTable');
        }
        return $this->homeTable;
    }
    public function indexAction()
    {
        return new ViewModel(
            array('homes' => $this->getHomeTable()->fetchAll())
        );
    }
    public function addAction()
    {
        $form = new HomeForm();
        $form->get('submit')->setValue('Add');
        
        $request = $this->getRequest();
        if ($request->isPost())
        {
            $home = new Home();
            $form->setInputFilter($home->getInputFilter());
            $form->setData($request->getPost());
            if ($form->isValid())
            {
                $home->exchangeArray($form->getData());
                $this->getHomeTable()->saveHome($home);
                
                // redirect to list home
                return $this->redirect()->toRoute('home');
            }
        }
        return array('form' => $form);
    }

    public function editAction()
    {
        $id = (int) $this->params()->fromRouter('id', 0);
        if (!$id)
        {
            return $this->redirect()->toRoute('home', array('action' => 'add'));
        }
    }
}
?>


<?php 
// module/Product/src/Product/Controller/ProductController.php
/*namespace Product\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class ProductController extends AbstractActionController
{
    public function indexAction()
    {
    }
    public function addAction()
    {
    }
    public function editAction()
    {
    }
    public function deleteAction()
    {
    }
}*/
?>
